<?php
class SettingController extends AdminAppController{
    function index()
    {
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Setting');

        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);
        
        $settings = $this->Setting->find('all');
        $setting = array();
        foreach($settings as $set)
        {
            $group = $set['Setting']['group'];
            $lang_code = $set['Setting']['lang_code'];
            $key = $set['Setting']['key'];
            $value = $set['Setting']['value'];
            if($lang_code != "")
            {
                $setting[$group][$key][$lang_code] = $value;
            }
            else
            {
                $setting[$group][$key] = $value;
            }
        }
        $this->set('setting', $setting);
        
        if($this->request->isPost())
        {
            $this->Setting->delAll();  
            $request = @$this->request->data();
            foreach($request as $group => $dt)
            {
                if(is_array($dt))
                {
                    foreach($dt as $key => $value)
                    {
                        if(is_array($value))
                        {
                            foreach($value as $lang_code => $v)
                            {
                                $data = array();
                                $data['key'] = $key;
                                $data['value'] = $v;
                                $data['group'] = $group;
                                $data['lang_code'] = $lang_code;
                                $this->Setting->add($data);
                            }
                        }
                        else
                        {
                            if($group != 'image')
                            {
                                $data = array();
                                $data['key'] = $key;
                                $data['value'] = $value;
                                $data['group'] = $group;
                                $this->Setting->add($data);
                            }
                            else
                            {
                                $data = array();
                                $data['key'] = $key;
                                $data['value'] = $this->Setting->edit_url_image($value);
                                $data['group'] = $group;
                                $this->Setting->add($data);
                            }
                        }
                    }
                }
            }
            $this->Session->setFlash(
                                'Cập nhật cấu hình thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            $this->redirect(array("controller" => "setting", 
                                  "action" => "index"));
        }
    }
}
